package com.alibaba.chaosblade.box.dao.infrastructure.manager;

import com.alibaba.chaosblade.box.common.infrastructure.util.CollectionUtil;
import com.alibaba.chaosblade.box.dao.model.SceneFunctionCategoryRelationDO;
import com.alibaba.chaosblade.box.dao.model.SceneFunctionDO;
import com.alibaba.chaosblade.box.dao.repository.SceneFunctionCategoryRelationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author haibin.lhb
 *
 *
 */
@Component
public class SceneFunctionCategoryManager {

    @Autowired
    private SceneFunctionCategoryRelationRepository sceneFunctionCategoryRelationRepository;

    /**
     * 重新绑定场景函数与类目的关联关系
     *
     * @param sceneFunctionDOS 场景函数列表，包含需要重新绑定类目关系的函数信息
     */
    public void rebindFunctionCategories(List<SceneFunctionDO> sceneFunctionDOS) {
        sceneFunctionDOS.forEach(sceneFunctionDO -> {
            sceneFunctionCategoryRelationRepository.deleteByFunctionCode(sceneFunctionDO.getCode());
            if (CollectionUtil.isNullOrEmpty(sceneFunctionDO.getCategoryList())) { return; }
            List<SceneFunctionCategoryRelationDO> sceneFunctionCategoryRelationDOS = sceneFunctionDO
                .getCategoryList().stream().map(
                    s -> {
                        SceneFunctionCategoryRelationDO sceneFunctionCategoryRelationDO
                            = new SceneFunctionCategoryRelationDO();
                        sceneFunctionCategoryRelationDO.setCode(sceneFunctionDO.getCode());
                        sceneFunctionCategoryRelationDO.setCategoryId(s);
                        sceneFunctionCategoryRelationDO.setFunctionId(sceneFunctionDO.getFunctionId());
                        return sceneFunctionCategoryRelationDO;
                    }).collect(Collectors.toList());
            if (!sceneFunctionCategoryRelationDOS.isEmpty()) {
                sceneFunctionCategoryRelationRepository.saveBatch(sceneFunctionCategoryRelationDOS);
            }
        });
    }

    /**
     * 根据类目ID获取关联的函数ID列表
     *
     * @param categoryId 类目ID
     * @return 返回与指定类目关联的函数ID列表
     */
    public List<String> getFunctionIdByCategoryId(String categoryId) {
        return sceneFunctionCategoryRelationRepository.findByCategoryId(categoryId).stream().map(SceneFunctionCategoryRelationDO::getFunctionId).collect(Collectors.toList());
    }
}
